<template>
    <div class="e-form">
        <BillTop title="维修保养记录新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.maintenanceLog" label-width="180px" :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="记录日期：" prop="billDate">
                            <el-date-picker
                                v-model="formData.maintenanceLog.billDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="维保类型：" prop="maintenanceType">
                            <el-select v-model="formData.maintenanceLog.maintenanceType"  @change="selectMaintenanceType">
                                <el-option :value="item.value" :label="item.label" v-for="item,index in maintenanceType" :key="index" ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="维保计划：" prop="maintenancePlan">
                            <el-input v-model="formData.maintenanceLog.maintenancePlan"  readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectMaintenancePlan"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="维保单位：" prop="maintenanceUnit">
                            <el-input v-model="formData.maintenanceLog.maintenanceUnit">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="机构：">
                            <span>{{ userInfo.orgInfo.orgName }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="送修/养日期：" prop="sendToRepairTime">
                            <el-date-picker
                                v-model="formData.maintenanceLog.sendToRepairTime"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="完修/养日期：" prop="completeRepairTime">
                            <el-date-picker
                                v-model="formData.maintenanceLog.completeRepairTime"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="维保费（元）：">
                            <span>{{ formData.maintenanceLog.maintenancePrice }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="辅助设备费（元）：">
                            <span>{{ formData.maintenanceLog.auxiliaryFee }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-row>
                    <el-col :span="12">
                        <el-form-item label="材料费（元）：">
                            <span>{{ formData.maintenanceLog.materialsPrice }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="人工费（元）：">
                            <span>{{ formData.maintenanceLog.manpowerPrice }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="记录人：">
                            <span>{{ formData.maintenanceLog.noteTaker }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：">
                            <span>{{ statusLable(formData.maintenanceLog.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose" v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/dailyManagement.js'
import { mapState } from 'vuex'
import { selectMaintenance } from '@/api/equip/popupCallback.js'
export default {
    data () {
        return {
            formData: {
                //基础信息
                'maintenanceLog': {
                    'auxiliaryFee': 0,
                    'billDate': '',
                    'billId': '',
                    'billNo': '',
                    'completeRepairTime': '',
                    'equipmentName': '',
                    'equipmentNo': '',
                    'equipmentPrice': 0,
                    'facilityInitializeId': '',
                    'founderId': '',
                    'founderName': '',
                    'isInit': true,
                    'maintenanceDetails': '',
                    'maintenancePlan': '',
                    'maintenancePlanId': '',
                    'maintenancePrice': 0,
                    'maintenanceType': null,
                    'maintenanceUnit': '',
                    'manpowerPrice': 0,
                    'materialsPrice': 0,
                    'noteTaker': '',
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDescription': '',
                    'nullifyReason': '',
                    'operateState': 0,
                    'orgId': '',
                    'orgName': '',
                    'remarks': '',
                    'sendToRepairTime': '',
                    'specifications': '',
                    'state': 0,
                    'workId': ''
                },
            },
            rules: {
                billDate: [
                    { required: true, message: '请选择记录日期', trigger: 'change' },
                ],
                maintenancePlan: [
                    { required: true, message: '请选择维保计划', trigger: 'change'  },
                ],
                maintenanceType: [
                    { required: true, message: '请选择维保类型', trigger: 'change'  },
                ],
                maintenanceUnit: [
                    { required: true, message: '请填写维保单位', trigger: 'change'  },
                ],
                sendToRepairTime: [
                    { required: true, message: '请选择送修/养日期', trigger: 'change'  },
                    { validator: (rule, value, callback)=>{
                        const sendToRepairTime = new Date(this.formData.maintenanceLog.sendToRepairTime).getTime() //送修/养日期
                        const  completeRepairTime = new Date(this.formData.maintenanceLog.completeRepairTime).getTime() //完修/养日期
                        if(this.formData.maintenanceLog.sendToRepairTime) {
                            if(sendToRepairTime > completeRepairTime) {
                                callback(new Error('送修/养日期不能大于完修/养日期！'))
                            }else{
                                callback()
                            }
                        }else{
                            callback()
                        }
                    }, trigger: 'change' }
                ],
                completeRepairTime: [
                    { required: true, message: '请选择完修/养日期', trigger: 'change'  },
                    { validator: (rule, value, callback)=>{
                        const sendToRepairTime = new Date(this.formData.maintenanceLog.sendToRepairTime).getTime() //送修/养日期
                        const  completeRepairTime = new Date(this.formData.maintenanceLog.completeRepairTime).getTime() //完修/养日期
                        if(this.formData.maintenanceLog.sendToRepairTime) {
                            if(sendToRepairTime > completeRepairTime) {
                                callback(new Error('完修/养日期不能小于送修/养日期！'))
                            }else{
                                callback()
                            }
                        }else{
                            callback()
                        }
                    }, trigger: 'change' }
                ],
            },
        }
    },
    components: {
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            maintenanceType: state => state.equip.equipData.maintenanceType, //维保类型
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return obj.label
            }
        },
    },
    created () {
        this.getDicValue()
        this.getInfo()
    },
    mounted () {
    },
    methods: {
        //获取当前使用的字典
        getDicValue () {
            this.$store.dispatch('equip/setMaintenanceType')//更新维保类型
        },
        getInfo () {
            const date = new Date()
            this.formData.maintenanceLog.billDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
            this.formData.maintenanceLog.noteTaker = localStorage.getItem('userName')
        },
        //切换维保类型
        selectMaintenanceType () {
            this.formData.maintenanceLog.maintenancePlan = ''
            this.formData.maintenanceLog.maintenancePlanId = ''
        },
        //选择维保计划
        selectMaintenancePlan () {
            if(this.formData.maintenanceLog.maintenanceType === '' || this.formData.maintenanceLog.maintenanceType === null) {
                this.clientPop('err', '请先选择为维保类型！')
                return
            }
            let json = {
                'type': this.formData.maintenanceLog.maintenanceType,
                'ids': [this.formData.maintenanceLog.maintenancePlanId]
            }
            selectMaintenance(json).then(res=>{
                this.formData.maintenanceLog.maintenancePlan = res[0].billNo
                this.formData.maintenanceLog.maintenancePlanId = res[0].billId
            })
        },
        //查看单据
        lookSource () {},
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if(valid) {
                    request.maintenanceRecordsAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/maintenanceRecordsEdit',
                                query: {
                                    billid: res,
                                    name: 'planDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                }else{
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/maintenanceRecordsList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
